module.exports = {
  /**
   * 查询多条记录 分页
   * @url admin/kong/sys/getList 前端调用的url参数地址
   * data 请求参数 说明
   * @param {Number}         pageIndex 当前页码
   * @param {Number}         pageSize  每页显示数量
   * @param {Array<Object>}  sortRule  排序规则
   * @param {object}         formData  查询条件数据源
   * @param {Array<Object>}  columns   查询条件规则
   * res 返回参数说明
   * @param {Number}         code      错误码，0表示成功
   * @param {String}         msg       详细信息
   */
  main: async (event) => {
    let { data = {}, userInfo, util, filterResponse, originalParam } = event;
    let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
    let { uid } = data;
    let res = { code: 0, msg: '' };
    // 业务逻辑开始-----------------------------------------------------------
    let dbName = "tm-detection-form";
    let _whereJson = undefined;
    if (!userInfo.role.includes('admin')) {
      if (userInfo.role.includes('ROLE_PSGS')) {
        _whereJson = {

          client_name: userInfo.nickname

        }
      } else if (userInfo.role.includes('ROLE_PT')) {
        // userInfo.role.includes('ROLE_ZZ') ||
        _whereJson = {

          client: _.in(userInfo.delivery_company),
          detection_user: userInfo.nickname
        }
      }
    }
    if (userInfo._id != '65f8237ffe975f7440b02b41') {
      if (_whereJson!=undefined&& _whereJson.client_name != undefined) {
        _whereJson =
          _.and([
            {
              client_name: _whereJson.client_name
            },
            {
              client_name: _.nin(['佛山市永信餐饮管理有限公司', '佛山市品鲜汇农产品有限公司', '肇庆市赵燕华蔬果有限公司', '端州区1688水果店', '四会市程晖蔬果种植农民专业合作社'])
            }
          ])
        
      }
      else {
        _whereJson = {
          ..._whereJson,
          client_name: _.nin(['佛山市永信餐饮管理有限公司', '佛山市品鲜汇农产品有限公司', '肇庆市赵燕华蔬果有限公司', '端州区1688水果店', '四会市程晖蔬果种植农民专业合作社'])
        };
      }

    }


    res = await vk.baseDao.getTableData({
      dbName,
      data,
      whereJson: _whereJson,
      foreignDB: [{
        dbName: "uni-id-users",
        localKey: "detection_user",
        foreignKey: "nickname",
        as: "detection_user_info",
        fieldJson: {
          _id: true,
          nickname: true,
          sign_image: true
        },
        limit: 1,
      },
      {
        dbName: "tm-sample-testing",
        localKey: "_id",
        foreignKey: "detectionform_id",
        as: "sample_list",
        sortArr: [
          { name: "sampleno", type: "asc" }
        ],
        limit: 100,
      }]
    });
    return res;
  }

}
